package com.tansun.easycare.rule.datasource.web;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.StringUtils;
import com.tansun.easycare.core.web.BaseController;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.ResMessage;
import com.tansun.easycare.rule.datasource.entity.DatasheetVo;
import com.tansun.easycare.rule.datasource.entity.RuleDatasheet;
import com.tansun.easycare.rule.datasource.entity.RuleDatasheetColumn;
import com.tansun.easycare.rule.datasource.service.RuleDatasheetColumnService;
import com.tansun.easycare.rule.datasource.service.RuleDatasheetService;
import com.tansun.easycare.rule.datasource.service.RuleDatasourceService;
import com.tansun.easycare.rule.front.entity.RuleDatasource;

/**
 * 入口参数Controller
 * @author WYH
 * @version 2018-06-12
 */
@Controller
@RequestMapping(value = "/ruleProj/datasource/datasheet")
public class RuleDatasheetController extends BaseController {

	private final Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private RuleDatasheetService ruleDatasheetService;
	@Autowired
	private RuleDatasourceService datasourceService;
	@Autowired
	private RuleDatasheetColumnService colService;
	
	@ModelAttribute
	public RuleDatasheet get(@RequestParam(required=false) String id) {
		RuleDatasheet entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ruleDatasheetService.get(id);
		}
		if (entity == null){
			entity = new RuleDatasheet();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(RuleDatasheet ruleDatasheet, HttpServletRequest request, HttpServletResponse response, Model model) {
		PageInfo<RuleDatasheet> page = ruleDatasheetService.findPage(new Page<RuleDatasheet>(), ruleDatasheet); 
		model.addAttribute("page", page);
		return "rule/datasource/ruleDatasheetList";
	}

	@RequestMapping(value = "form")
	public String form(RuleDatasheet entity, Model model) {

		model.addAttribute("datasources", datasourceService.findList(new RuleDatasource()));
		model.addAttribute("entity", entity);
		if(StringUtils.isNotBlank(entity.getId())){
			RuleDatasheetColumn col = new RuleDatasheetColumn();
			col.setDatasheetId(entity.getId());
			model.addAttribute("cols", colService.findList(col));
		}
		return "rule/datasource/ruleDatasheetForm";
	}

	
	/**
	 * 根据数据源ID，获取数据源下所有表名称
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getDbTable")
	@ResponseBody
	public JsonMessage getDbTable(String id) {
		JsonMessage result = new JsonMessage();
		try {
			RuleDatasource ruleDatasource = datasourceService.get(id);
			List<RuleDatasheet> list = ruleDatasheetService.findDbTable(ruleDatasource);
			result.setData((Serializable) list);
		} catch (Exception e) {
			log.error("获取数据库表出错",e);
			result.setResult(false);
		}
		return result;
	}
	/**
	 * 获取数据库表的列信息
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "getTableCol")
	@ResponseBody
	public JsonMessage getTableCol(RuleDatasheet entity) {
		JsonMessage result = new JsonMessage();
		try {
			List<RuleDatasheetColumn> list = ruleDatasheetService.findTableCols(entity);
			result.setData((Serializable) list);
		} catch (Exception e) {
			log.error("获取列信息出错",e);
			result.setResult(false);
		}
		return result;
	}

	@RequestMapping(value = "save")
	@ResponseBody
	public ResMessage save(DatasheetVo vo) {
		ResMessage msg = new ResMessage();
		try {
			ruleDatasheetService.saveOrUpdate(vo);
			msg.setCode("1");
		} catch (Exception e) {
			log.error("保存数据源出错", e);
			msg.setCode("0");
            msg.setMsg(e.getMessage());
		}
		return msg;
	}
	

	@RequestMapping("delete")
    public @ResponseBody
    JsonMessage statusChange(RuleDatasheet entiy) throws Exception {
        JsonMessage ret = new JsonMessage();
        try {
        	ruleDatasheetService.delete(entiy);
        } catch (Exception e) {
        	log.error("删除数据表出错", e);
            ret.setResult(false);
            ret.setMessage(e.getMessage());
        }
        return ret;
    }

}